package com.daz.student.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class upLoadPicAction extends ActionSupport{
	
	private String PicType;
	private String uploadFileName;
	private String path;
	private File upload;
	private String photoWidth;
	private String photoHeight;
	
	
	private Boolean success;
	private String msg;
	
	public String photoPath(){
		
		String imgpath=PicType+"\\";
		String path = ServletActionContext.getServletContext().getRealPath("/");
		File pathFile = new File(path+"pic\\");
		if(!pathFile.isDirectory()){
			pathFile.mkdir();
		}
		return path+"pic\\"+imgpath;
	}
	public String uploadPhotoByType(){
		 SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
		 String str = sdf.format(new Date());
		 str = str.replace(":", "");
		 str = str.replace("-", "");
		 String[] nameS = uploadFileName.split("\\.");
		 uploadFileName = nameS[0]+str + "."+nameS[1];
		String realPath =  photoPath()+	uploadFileName;	
		//realPath = realPath.replace(" ", "");
        if (upload != null) {
        	File pathFile = new File(photoPath());
    		if(!pathFile.isDirectory()){
    			pathFile.mkdir();
    		}
        	try { 
        		BufferedInputStream bis = new BufferedInputStream( 
                    new FileInputStream(upload)); 
        		BufferedOutputStream bos = null; 
                bos = new BufferedOutputStream(new FileOutputStream(realPath));
                byte[] buff = new byte[8192]; 
                for (int len = -1; (len = bis.read(buff)) != -1;) { 
                    bos.write(buff, 0, len); 
                } 
                bos.flush(); 
                bis.close(); 
                bos.close();
                this.setSuccess(true);
                this.setPath("pic\\"+PicType+"\\"+uploadFileName);
            } catch (Exception e) { 
                e.printStackTrace(); 
                this.setSuccess(false);               
            }
                                                                 
        } 
        else{
        	this.setSuccess(false);
        }
		return SUCCESS;
	}
	public String getPicType() {
		return PicType;
	}
	public void setPicType(String picType) {
		PicType = picType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getPhotoWidth() {
		return photoWidth;
	}
	public void setPhotoWidth(String photoWidth) {
		this.photoWidth = photoWidth;
	}
	public String getPhotoHeight() {
		return photoHeight;
	}
	public void setPhotoHeight(String photoHeight) {
		this.photoHeight = photoHeight;
	}
	public Boolean getSuccess() {
		return success;
	}
	public void setSuccess(Boolean success) {
		this.success = success;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
	
	
}
